/*
* pps_bom
*/
package com.pitop.assembly.service;

import com.pitop.assembly.api.AssemblyCategoryFeignApi;
import com.pitop.assembly.entity.AssemblyCategory;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.AssemblyCategoryException;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


/**
 * 组件类型
 *
 * @author berton
 */
@Service("assemblyCategorySVImpl")
@Slf4j
public class AssemblyCategorySVImpl {

    @Autowired
    private AssemblyCategoryFeignApi assemblyCategoryFeignApi;

    /**
     * 加载一个对象AssemblyCategory
     *
     * @param id * @param code 编码
     * @return AssemblyCategory
     */
    public AssemblyCategory load(Long id, String code) {
        if (id == null && code == null) {
            throw new AssemblyCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyCategoryFeignApi.load(id, code);
    }

    /**
     * 加载一个对象AssemblyCategory 通过id,(将查询关联数据)
     *
     * @param id
     * @return AssemblyCategory
     */
    public AssemblyCategory loadById(Long id) {
        if (id == null) {
            throw new AssemblyCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyCategoryFeignApi.loadById(id);
    }

    /**
     * 加载一个对象AssemblyCategory 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return AssemblyCategory
     */
    public AssemblyCategory loadByCode(String code) {
        if (code == null) {
            throw new AssemblyCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyCategoryFeignApi.loadByCode(code);
    }

    /**
     * 加载一个对象AssemblyCategory,(将查询关联数据)
     *
     * @param id * @param code 编码
     * @return AssemblyCategory
     */
    public AssemblyCategory get(Long id, String code) {
        if (id == null && code == null) {
            throw new AssemblyCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyCategoryFeignApi.get(id, code);
    }

    /**
     * 加载一个对象AssemblyCategory 通过id,(将查询关联数据)
     *
     * @param id
     * @return AssemblyCategory
     */
    public AssemblyCategory getById(Long id) {
        if (id == null) {
            throw new AssemblyCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyCategoryFeignApi.getById(id);
    }

    /**
     * 加载一个对象AssemblyCategory 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return AssemblyCategory
     */
    public AssemblyCategory getByCode(String code) {
        if (code == null) {
            throw new AssemblyCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyCategoryFeignApi.getByCode(code);
    }

    /**
     * 删除对象AssemblyCategory
     *
     * @param id * @param code 编码
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new AssemblyCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        assemblyCategoryFeignApi.delete(id, code);
    }

    /**
     * 查询AssemblyCategory分页
     *
     * @param assemblyCategory 对象
     * @param curPage          当前页
     * @param pageSize         分页条数
     * @return List<AssemblyCategory>
     */
    public List<AssemblyCategory> list(AssemblyCategory assemblyCategory, String isShowAll, int curPage, int pageSize) {
        return assemblyCategoryFeignApi.list(assemblyCategory,isShowAll, curPage, pageSize);
    }

    /**
     * 查询AssemblyCategory分页
     *
     * @param id       * @param code  编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<AssemblyCategory>
     */
    public List<AssemblyCategory> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        AssemblyCategory assemblyCategory = new AssemblyCategory();
        assemblyCategory.setId(id);
        assemblyCategory.setCode(code);

        return assemblyCategoryFeignApi.list(assemblyCategory, null, curPage, pageSize);
    }

    public int count(Long id, String code) {
        return assemblyCategoryFeignApi.count(id, code);
    }

    public int count(AssemblyCategory assemblyCategory) {
        return assemblyCategoryFeignApi.count(assemblyCategory);
    }

    /**
     * 保存
     *1.参数合法性验证
     *2.分类名称是否存在
     * @param assemblyCategory 实体
     * @throws BaseException
     */
    public void save(AssemblyCategory assemblyCategory) {
        if (assemblyCategory == null) {
            throw new AssemblyCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        AssemblyCategory flag = assemblyCategoryFeignApi.loadByName(assemblyCategory.getName());
        if(flag != null) {
            throw new AssemblyCategoryException(BaseException.BaseExceptionEnum.Exists);
        }
        assemblyCategoryFeignApi.build(assemblyCategory);
    }

    /**
     * 更新
     *
     * @param assemblyCategory 实体
     * @throws BaseException
     */
    public void modify(AssemblyCategory assemblyCategory) {
        if (assemblyCategory == null) {
            throw new AssemblyCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        AssemblyCategory flag = assemblyCategoryFeignApi.loadByName(assemblyCategory.getName());
        if(flag != null && !assemblyCategory.getCode().equals(flag.getCode())) {
            throw new AssemblyCategoryException(BaseException.BaseExceptionEnum.Exists);
        }
        assemblyCategoryFeignApi.modify(assemblyCategory);
    }

    /**
     * 修改组件类型状态
     *
     * @throws BaseException
     */
    public void updateState(@RequestParam("code") String code, @RequestParam("state") String state) {
        if (StringTools.isEmpty(code) || StringTools.isEmpty(state)) {
            throw new AssemblyCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        assemblyCategoryFeignApi.updateState(code, state);
    }
}
